<?php
# certain functions that help with what we need to do
function bugCreateHelper($reproducibility, $severity, $priority, $summary, $description, $project_id, $reporter_id){    
    # Change this path to point to the Mantis installation core.php file
    
   	require_once('../core.php');

	$t_core_path = config_get('core_path');

    require_once($t_core_path.'bug_api.php');

	access_ensure_project_level(config_get('report_bug_threshold'));

	$t_bug_data = new BugData;

	$t_bug_data->view_state			= config_get('default_bug_view_status');
	$t_bug_data->reproducibility	= $reproducibility;
	$t_bug_data->severity			= $severity;
	$t_bug_data->priority			= $priority;
	$t_bug_data->summary			= $summary;
	$t_bug_data->description		= $description;
	$t_bug_data->project_id			= $project_id;
	$t_bug_data->reporter_id		= user_get_id_by_name($reporter_id);
	
	if($t_bug_data->reporter_id == ""){
    	$tmp = "Reported by: " . $reporter_id . "\n---------------------------------------------------\n\n";
    	$tmp .= $t_bug_data->description;
    	$t_bug_data->description = $tmp;
	}
	
	$t_bug_data->summary			= trim($t_bug_data->summary);
	
	# Create the bug
	$t_bug_id = bug_create($t_bug_data);

    email_new_bug($t_bug_id);
    
    return $t_bug_id;

}


function getMantisBugById($bug_id){
	
	$bug = bug_get($bug_id);
	
	$bug_array = get_object_vars($bug);
	$bug_array["id"] = $bug_id;
	$bug_extra = addExtraBugData($bug_array);
	
	return $bug_extra;
}

function getMantisBugByProjectID($project_id, $status=""){
	

	$page_number = 0;
	$per_page = 20;
	$page_count = 0;
	$bug_count = 20;
	
	$result = filter_get_bug_rows($page_number, $per_page, $page_count, $bug_count, "", $project_id);
	
	$bugs = array();
	foreach($result as $r){
		$bugs[] = addExtraBugData($r);	
	}
	
	if(count($bugs) == 0){
		return $project_id;
	}
	
	return $bugs;

}

function addExtraBugData($bug){
	
	$bug["project_name"] = project_get_name($bug["project_id"]);
	
	if($bug["reporter_id"] != ""){
		$bug["reporter_name"] = user_get_field($bug["reporter_id"],'username');
	}

	$bug["severity_name"] = get_enum_element('severity', $bug["severity"]);
	$bug["priority_name"] = get_enum_element('priority', $bug["priority"]);
	$bug["status_name"] = get_enum_element('status', $bug["status"]);
	$bug["reproducibility_name"] = get_enum_element('reproducibility', $bug["reproducibility"]);
	
	if($bug["handler_id"] == ""){
		$bug["handler_name"] = user_get_field($bug["handler_id"],'username');
	}

	$bug["projection_name"] = get_enum_element('projection', $bug["projection"]);
	$bug["eta_name"] = get_enum_element('eta', $bug["eta"]);
	$bug["resolution_name"] = get_enum_element('resolution', $bug["resolution"]);
	$bug["description"] = bug_get_text_field($bug["id"], 'description');

	return $bug;
}

function getMantisBugByProjectName($project_name, $status=""){
	$project_id = project_get_id_by_name($project_name);
	
	if($project_id == 0){
		return 0;
	}
	
	return getMantisBugByProjectId($project_id, $status);
}

function updateMantisBugStatusById($bug_id, $status){
	$bug = bug_get($bug_id);
	$bug->status = $status;
	
	$result = bug_update($bug_id, $bug);
	
	return $result;
}